<template>
  <div class="main">
    <div class="header">
      <div>
        <i class="iconfont icon-back"></i>
      </div>
      <div>
        <h2>购物车</h2>
      </div>
      <div>
        <p>管理</p>
      </div>
    </div>
    <div class="content">
      <ul class="shop-list">
        <li v-for="(item,index) in shopCart" v-bind:key="index+1">
          <div class="item-1">
            <input v-on:click="item.isCheck=!item.isCheck" class="check-box" type="checkbox" v-bind:checked="item.isCheck">
          </div>
          <div class="item-2">
            <img class="shop-image" v-bind:src="item.imgSrc" alt="商品">
          </div>
          <div class="item-3">
            <span class="shop-name">{{item.title}}</span>
            <span class="shop-norm">规格：{{item.spec}}</span>
            <span class="shop-price">￥{{item.price}}</span>
          </div>
          <div class="item-4">
            <div class="del">
              <i class="iconfont icon-delete" v-on:click="delItem(index)"></i>
            </div>
            <div class="num">
              <span v-on:click="item.num<=1?item.num:item.num--">-</span>
              <input class="num-input" type="text" v-model="item.num" @keyup="regex(index)" @blur="regex(index)">
              <span v-on:click="item.num++">+</span>
            </div>
          </div>
        </li>
      </ul>
    </div>
    <div class="total">
      <div class="item-1">
        <input v-on:click="setCheckAll()" class="check-box" type="checkbox" v-bind:checked="isCheckAll">
        <span class="all-text">全选</span>
      </div>
      <div class="item-2">
        <span class="select-num">已选{{selectNum}}件</span>
      </div>
      <div class="item-3">
        <span class="total-price">
          <i>合</i>
          <i>计</i>
          <b>￥{{totalPrice}}</b>
        </span>
      </div>
      <div class="item-4">
        <button class="submit" type="button">结算</button>
      </div>
    </div>
    <Footer></Footer>
  </div>
</template>

<script>
import Footer from "../template/footer";
export default {
  name: "shopCartComponent",
  data() {
    return {
      shopCart: [
        {
          id: 1,
          title: "诺曼姿薰衣草精油",
          spec: "5ml",
          price: 339,
          num: 1,
          imgSrc: require("../image/7556thfa6guwvygth01qtbm27vs6u7.jpg"),
          isCheck: true
        },
        {
          id: 2,
          title: "诺曼姿玫瑰香护手霜",
          spec: "5ml",
          price: 389,
          num: 1,
          imgSrc: require("../image/7556thfa6guwvygth01qtbm27vs6u7.jpg"),
          isCheck: true
        },
        {
          id: 3,
          title: "诺曼姿冬虫夏草修复液",
          spec: "5ml",
          price: 899,
          num: 1,
          imgSrc: require("../image/7556thfa6guwvygth01qtbm27vs6u7.jpg"),
          isCheck: true
        }
      ]
    };
  },
  components: {
    Footer
  },
  computed: {
    totalPrice() {
      let total = 0;
      this.shopCart.forEach(item => {
        if (item.isCheck) {
          total += item.num * item.price;
        }
      });
      return total;
    },
    isCheckAll() {
      return this.shopCart.every(item => item.isCheck);
    },
    selectNum() {
      let num = 0;
      this.shopCart.forEach(item => {
        if (item.isCheck) {
          num++;
        }
      });
      return num;
    }
  },
  methods: {
    setCheckAll() {
      if (this.isCheckAll) {
        this.shopCart.forEach(item => {
          item.isCheck = false;
        });
      } else {
        this.shopCart.forEach(item => {
          item.isCheck = true;
        });
      }
    },
    regex(index) {
      let num = this.shopCart[index].num;
      this.shopCart[index].num = num
        ? num.replace(/^0*/, "").replace(/[^0-9]*/g, "")
        : "1";
    }
  }
};
</script>

<style scoped lang="less">
.main {
  color: #ffffff;
}
.header {
  display: flex;
  height: 80px;
  background-color: #6a7531;
  div {
    width: 33.33%;
    height: 80px;
    line-height: 80px;
    font-size: 36px;
    h2 {
      text-align: center;
    }
    i {
      font-size: 52px;
    }
  }
  div:first-child {
    text-align: left;
    i {
      margin-left: 15px;
    }
  }
  div:last-child {
    text-align: right;
    p {
      float: right;
      margin-right: 15px;
      font-size: 24px;
    }
  }
}
.content {
  .shop-list {
    list-style: none;
    li {
      display: flex;
      height: 218px;
      margin: 30px 0;
      background-color: #f4f4f4;
      .item-1 {
        width: 15%;
      }
      .item-2 {
        width: 30%;
      }
      .item-3 {
        width: 35%;
      }
      .item-4 {
        width: 20%;
      }
      .check-box {
        float: left;
        width: 26px;
        height: 26px;
        margin: 96px 50px 96px 18px;
      }
      .shop-image {
        width: 202px;
        height: 202px;
        border: #9f9797 1px solid; /*no*/
        margin: 7px 0 7px 0;
      }
      .shop-name,
      .shop-norm,
      .shop-price {
        display: block;
        color: #333333;
        font-size: 30px;
        margin-left: 20px;
        margin-top: 25px;
      }
      .shop-price {
        color: #fb0303;
        font-size: 36px;
      }
      .del {
        color: #333333;
        text-align: right;
        i {
          display: block;
          font-size: 42px;
          margin-top: 5px;
          margin-right: 15px;
        }
      }
      .num {
        float: right;
        margin-top: 126px;
        margin-right: 15px;
        span {
          float: left;
          width: 26px;
          height: 27px;
          color: #333333;
          line-height: 27px;
          text-align: center;
          font-size: 24px;
          border: #ababab 1px solid; /*no*/
        }
        input {
          float: left;
          width: 53px;
          height: 27px;
          border: none;
          outline: none;
          color: #333333;
          line-height: 27px;
          text-align: center;
          padding: 0;
          border-top: #ababab 1px solid; /*no*/
          border-bottom: #ababab 1px solid; /*no*/
          background-color: transparent;
        }
      }
    }
  }
}
.total {
  position: fixed;
  bottom: 99px;
  display: flex;
  width: 100%;
  height: 100px;
  line-height: 30px;
  color: #333333;
  font-size: 30px;
  border-top: #a6a3a3 1px solid; /*no*/
  .item-1 {
    width: 24%;
  }
  .item-2 {
    width: 18%;
  }
  .item-3 {
    width: 29%;
  }
  .item-4 {
    width: 29%;
  }
  .check-box {
    float: left;
    width: 26px;
    height: 26px;
    margin: 38px 24px 36px 18px;
  }
  span {
    float: left;
    margin: 35px 0 35px 0;
  }
  .all-text {
    margin-right: 45px;
  }
  .select-num {
    margin-right: 27px;
  }
  .total-price {
    display: flex;
    margin-top: 0;
    margin-bottom: 0;
    i {
      display: block;
      line-height: 100px;
    }
    b {
      display: block;
      line-height: 100px;
      color: #fb0303;
    }
  }
  .submit {
    float: right;
    width: 220px;
    height: 100px;
    border: none;
    outline: none;
    font-size: 36px;
    color: #ffffff;
    background-color: #fb0303;
  }
}
.footer {
  position: fixed;
  bottom: 0;
  width: 100%;
  height: 98px;
  list-style: none;
  border-top: #e4e4e4 1px solid; /*no*/
  display: flex;
  li {
    width: 20%;
    color: #444444;
    font-size: 24px;
    text-align: center;
    i {
      display: block;
      font-size: 42px;
      margin: 10px 0;
    }
  }
  li:hover {
    color: #6a7531;
  }
}
</style>